package com.kuang.web.admin;

import com.kuang.pojo.User;
import com.kuang.service.*;
import com.kuang.util.MD5Util;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private CommentService commentService;

    @RequestMapping("/toindex")
    public String toindex(Model model, HttpSession session) {
        int countTag = tagService.countTag();
        int countType = typeService.countType();
        int countBlog = blogService.countBlog();
        User user1 = userService.selectAdminUser(1);
        int countComment = commentService.countComment();
//        model.addAttribute("user1",user1);
        session.setAttribute("user1", user1);
        model.addAttribute("counttag", countTag);
        model.addAttribute("counttype", countType);
        model.addAttribute("countblog", countBlog);
        model.addAttribute("countcomment", countComment);
        return "admin/dashboard";
    }

    @RequestMapping("/")
    public String tologin() {
        return "admin/login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(String username, String password, HttpSession session) {
        User user = userService.login(username, MD5Util.code(password));
        if (user != null) {   // 登录成功
            session.setAttribute("user", user);
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "admin/login";
    }

    //configuration
    @RequestMapping("/toconfig")
    public String toconfig() {
//        System.out.println("Test");
        return "admin/configuration";
    }

    @RequestMapping("/mogifysite")
    public String modifysite(Model model, User user, HttpSession session) {
//        System.out.println(user);
        user.setUpdateTime(new Date());
        int i = userService.updateUser(user);
//        System.out.println("i = " + i);
        User user1 = userService.selectUser();  // 因为在配置页我们是用 登录时的存放在session的user对象 修改后session没更新
        session.removeAttribute("user");
        session.setAttribute("user", user1);      //  那么数据回显就还是之前的！
        return "redirect:/admin/toconfig";

    }

    @RequestMapping("/checkOldPwd")
    @ResponseBody    // ajax 验证新密码 和 新密码是否一致
    public String checkOldPwd(String oldpwd) {
        User user = userService.checkOldPwd(MD5Util.code(oldpwd));
        if (user != null) {  // 密码重复
            return "error";
        } else {
            return "success";
        }
//        return "";
    }

    @RequestMapping("/updatepwd")
    @ResponseBody
    public String updatePwd(String newPwd, Integer id, HttpSession session) {
//        System.out.println(newPwd);
        int i = userService.updatepwd(MD5Util.code(newPwd), 1, new Date());
        User user = userService.selectUser();
        if (i > 0) {
            session.removeAttribute("user");
//            session.setAttribute("user", user);
//            return "redirect:/admin/";  // 跳转到登录页面
            return "success";
        } else {
//            return "redirect:/admin/toconfig";  // 修改失败！
            return "error";
        }

        }

    }
